package com.qianfeng.missfood.fragment;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.missfood.BaseApp;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.GuidePagerAdapter;
import com.qianfeng.missfood.bean.CookContent;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.constants.UrlConstants;
import com.qianfeng.missfood.ui.LoginActivity;
import com.qianfeng.missfood.ui.RegisterActivity;
import com.qianfeng.missfood.utils.AddDotsUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 15-8-17.
 */
public class CookFragment extends BaseAppFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {

    private ArrayList<ImageView> imageViews;
    private GuidePagerAdapter guidePagerAdapter;
    //    private Button dotBtn;
    private FrameLayout cookFrameLayout;
    private ViewPager cookIntroViewPager;
    private TextView toLogin;
    private Button toRegister;
//    private LinearLayout linearLayout;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_cook_layout;
    }

    @Override
    public void init() {

        imageViews = new ArrayList<>();
        guidePagerAdapter = new GuidePagerAdapter(imageViews, getActivity());


    }

    @Override
    public void initView(View view) {

        cookIntroViewPager = (ViewPager) view.findViewById(R.id.cookIntroViewPager);
        cookIntroViewPager.setAdapter(guidePagerAdapter);
        //       cookIntroViewPager.addOnPageChangeListener(this);

        cookFrameLayout = (FrameLayout) view.findViewById(R.id.cookFrameLayout);

        toLogin = (TextView) view.findViewById(R.id.toLogin);
        toLogin.setOnClickListener(this);
        TextView introduce = (TextView) view.findViewById(R.id.introduce);
        introduce.setOnClickListener(this);
        toRegister = (Button) view.findViewById(R.id.toRegister);
        toRegister.setOnClickListener(this);

    }

    @Override
    public void onStart() {
        super.onStart();

        if (BaseApp.getInstance().getLoginState()) {

            toLogin.setVisibility(View.GONE);
            toRegister.setText("立即成为私厨");
        }
    }

    @Override
    public void initData() {

        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, UrlConstants.cookUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                List<CookContent> cookContentList = JSON.parseArray(result, CookContent.class);
                for (int i = 0; i < cookContentList.size(); i++) {

                    ImageView image = new ImageView(getActivity());

                    bitmapUtils.display(image, cookContentList.get(i).getUrl());

                    imageViews.add(image);

                }

                guidePagerAdapter.notifyDataSetChanged();
//                addDots();
                new AddDotsUtils(getActivity(), cookFrameLayout, cookIntroViewPager, imageViews, R.mipmap.custom_viewpage_point_focused, R.mipmap.custom_viewpage_point_unfocused);

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });

    }

//    private void addDots() {
//
//        linearLayout = new LinearLayout(getActivity());
//        FrameLayout.LayoutParams fl = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        fl.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
//        fl.setMargins(0, 0, 0, 30);
//        linearLayout.setLayoutParams(fl);
//        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//
//        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.custom_viewpage_point_unfocused);
//        for (int i = 0; i < imageViews.size(); i++) {
//
//            Button bt = new Button(getActivity());
//            LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(bitmap.getWidth(), bitmap.getHeight());
////            bt.setLayoutParams(new ViewGroup.LayoutParams(bitmap.getWidth(), bitmap.getHeight()));
//            ll.setMargins(3, 0, 3, 0);
//            bt.setLayoutParams(ll);
//            bt.setBackgroundResource(R.mipmap.custom_viewpage_point_unfocused);
//            linearLayout.addView(bt);
//
//        }
//
//        cookFrameLayout.addView(linearLayout);
//
//
//    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

//        if (dotBtn != null) {
//
//            dotBtn.setBackgroundResource(R.mipmap.custom_viewpage_point_unfocused);
//        }
//
//        Button currenButton = (Button) linearLayout.getChildAt(position);
//        currenButton.setBackgroundResource(R.mipmap.custom_viewpage_point_focused);
//        dotBtn = currenButton;

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.toLogin:

                startAction(LoginActivity.class);
                break;

            case R.id.introduce:

                break;

            case R.id.toRegister:

                if (BaseApp.getInstance().getLoginState()) {


                } else {

                    startAction(RegisterActivity.class);
                }

                break;

            default:

                break;


        }

    }

    private void startAction(Class<?> cls) {

        Intent intent = new Intent(getActivity(), cls);
        startActivityForResult(intent, AppConfig.COOK_REQUEST);

    }
}
